module AutoBase
  class Sequence < Array
    def initialize(session)
      @session = session
      super()
    end

    def add(action, param = {})
      result_of_added = action.added(@session, param)
      self << action
      result_of_added
    end

    def add_all(*actions)
      actions.each do |action|
        add(action)
      end
    end

    def delete(action, param = {})
      args = {
        :rescue => true,
        :apply => true
      }.update(param)

      result_of_deleted = nil

      if args[:rescue]
        if args[:rescue]
          begin
            result_of_deleted = action.deleted(@session, param)
          rescue Exception
          end
        else
          result_of_deleted = action.deleted(@session, param)
        end
      end

      super(action)
      result_of_deleted
    end

    def delete_all(param = {})
      args = {
        :reverse => true,
        :rescue => true,
        :apply => true
      }.update(param)

      actions = args[:reverse] ? self.reverse : self
      actions.each do |action|
        delete(action, args)
      end
    end
  end
end